package datautils;

//import com.romaremez.testuser.src.questionanswer.*;
import questionanswer.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: HAiL
 * Date: 14.11.13
 * Time: 19:47
 * To change this template use File | Settings | File Templates.
 */



public class DataFiller1 {

    private Question[] questions;
    private int size = 0;

    public DataFiller1() {
        int defaultCapacity = 1;
        questions = new Question[defaultCapacity];
    }


    public static void main(String[] args) throws IOException {
        new DataFiller1().writeQuestions();
    }



    private void addToQuestionArray(String question, String[] answers, String type, String theme) {
        addToQuestionArray(question, answers, 1, type, theme);
    }


    private void addToQuestionArray(String question, String[] answers, int correctAnswersNumber, String type, String theme) {
        int minCapacity = size + 1;
        if ((minCapacity - questions.length) > 0) {
            grow();
        }
        Answer answer = createAnswer(answers, correctAnswersNumber, type);
        questions[size++] = new Question(question, answer, theme);
    }


    private Answer createAnswer(String[] answers, int correctAnswersNumber, String type) {
        switch (type){
            case "Текстовый ответ":
                return new OneAnswer(answers[0]);
            case "Один ответ":
                return new OneAnswerFromMany(answers[0], answers);
            case "Несколько ответов":
                String[] correctAnswers = new String[correctAnswersNumber];
                System.arraycopy(answers, 0, correctAnswers, 0, correctAnswersNumber);
                return new MultipleAnswer(correctAnswers, answers);
            default:
                return null;  //TODO something here
        }
    }

    private void grow() {
        int oldCapacity = questions.length;
        int newCapacity = oldCapacity + 1;
        questions = Arrays.copyOf(questions, newCapacity);
    }

    private void writeFile() throws IOException {
        File dataHolder = new File("questions1.csv");
        ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(dataHolder));
        LinkedList<Question> qList = new LinkedList<>(Arrays.asList(questions));
        writer.writeObject(qList);

    }

    /**
     * Adds questions and answers to <code>Question[]</code> array.
     *
     */
    private void writeQuestions() throws IOException {
        String[] answers;
        String  question;

        answers = new String[1];
        question = "Переведите на английский слово 'параметризация'";
        answers[0] = "parametrization";
        addToQuestionArray(question, answers, "Текстовый ответ", "Общеобразовательное");

        answers = new String[4];
        question = "Назовите президентов РФ";
        answers[0] = "Путин";
        answers[1] = "Медведев";
        answers[2] = "Алан Кэй";
        answers[3] = "Джесика Альба";
        addToQuestionArray(question, answers, 2,"Несколько ответов", "Общеобразовательное");

        answers = new String[4];
        question = "Почему канализационные люки круглые, а не квадратные?";
        answers[0] = "Чтоб не падали";
        answers[1] = "Можно катить одному человеку";
        answers[2] = "Круглым щитом удобнее защищаться";
        answers[3] = "Круг символизирует бесконечность";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        answers = new String[4];
        question = "2+2=?";
        answers[0] = "4";
        answers[1] = "2";
        answers[2] = "8";
        answers[3] = "6";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        answers = new String[4];
        question = "2+5=7";
        answers[0] = "Да";
        answers[1] = "Нет";
        answers[2] = "-";
        answers[3] = "-";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        answers = new String[4];
        question = "2+3=?";
        answers[0] = "5";
        answers[1] = "4";
        answers[2] = "8";
        answers[3] = "6";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        answers = new String[4];
        question = "2+4=?";
        answers[0] = "6";
        answers[1] = "4";
        answers[2] = "8";
        answers[3] = "5";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        answers = new String[4];
        question = "2+7=?";
        answers[0] = "9";
        answers[1] = "4";
        answers[2] = "8";
        answers[3] = "6";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        answers = new String[4];
        question = "2+8=?";
        answers[0] = "10";
        answers[1] = "4";
        answers[2] = "8";
        answers[3] = "6";
        addToQuestionArray(question, answers, "Один ответ", "Общеобразовательное");

        writeFile();
    }
}

